<?php
namespace Agent\Controller;
use Think\Controller;
use Think\Image;
class SiteController extends PublicController {


    function _initialize() {
        parent::_initialize ();
    }
    //商务列表
    public function index(){
        $admin_id = $_SESSION["agentInfo"]["admin_id"];
        if(empty($admin_id)){
            $this->error('信息错误');exit;
        }
        $search = I('search');
        if($search){
            // $where['user_name|w.site'] = array('like',"%$search%");
            $wheresite = "and (u.user_name like '%$search%' or  w.site like '%$search%')";
            $this->assign("search", $search);
        }

        if(I('p')){
            $p = I('p');
        }else{
             $p = 1;
        }
        $andwhere = 'and w.status=1 and w.admin_id='.$admin_id.'';
        $header = C('header')['siteList'];
        $sql = "select w.id,w.site,u.user_name,count(w.keyword) as da,count(case when w.last_rank <= 10 and w.last_rank > 0 then w.keyword end) as dalv,u.user_id,FROM_UNIXTIME(w.logtime,'%Y-%m-%d %H:%i:%s') from pt_worlddetails as w,pt_user as u where w.user_id = u.user_id {$andwhere} {$wheresite} GROUP BY w.site,w.user_id";
        $list = M()->query($sql);
        $count = count($list);
        foreach ($list as $k => $v) {
            $list[$k]['user_id'] = round($v['dalv']/$v['da']*100,2).'％';
            $list[$k]['ftp'] = "<a href='#' user_id='{$v['user_id']}' class='details'>FTP</a>";
        }


        // $list = array(
        //     array(1,'www.bonnidee.cn','邦伲德','252','228',round(228/252*100,2).'％','2019-03-29 18:15:31',"<a href='#' user_id='1' class='details'>FTP</a>"),
        //     array(2,'www.shbdd56.com','上海包答答','351','298',round(298/351*100,2).'％','2019-03-29 18:15:31',"<a href='#' user_id='403' class='details'>FTP</a>"),
        //     array(3,'www.shtuoshun.com','拓顺','283','265',round(265/283*100,2).'％','2019-03-29 18:15:31',"<a href='#' user_id='577' class='details'>FTP</a>"),
        //     array(4,'www.sy-zymy.com','百馨','269','258',round(258/269*100,2).'％','2019-03-29 18:15:31',"<a href='#' user_id='584' class='details'>FTP</a>"),
        // );

        $Page = new \Think\Page($count, 30);
        $Page->setConfig('theme', "%UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%");
        $show = $Page->show(); // 分页显示输出


        $show = str_replace('<div>','',$show);
        $show = str_replace('current','active',$show);
        $show = str_replace('<<<','上一页<',$show);
        $show = str_replace('>>>','>下一页',$show);
        $show = str_replace('</div>','',$show);
        if($count <= 10){
            $show = '<a href="" class="active">1</a>';
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("p", $p);
        $this->assign("header", $header);
        $this->assign("list", $list);
        $this->assign("navs", 'Site');
        layout('Layout/twolayout');
        $this->display();
    }


     public function edit(){
        $id = I('id');
        if(empty($id)){
            $this->eroor("信息错误");exit;
        }
        $res = I('');
        if(empty($res['site']) || $res['ftp_ip'] || empty($res['username']) || $res['password'] || empty($res['adminadd']) || $res['adminusername'] || empty($res['adminpassword']) || $res['note']){
             $this->eroor("参数不全");exit;
        }
        $update['site'] = $res['site'];
        $update['ftp_ip'] = $res['ftp_ip'];
        $update['username'] = $res['username'];
        $update['password'] = $res['password'];
        $update['adminadd'] = $res['adminadd'];
        $update['adminusername'] = $res['adminusername'];
        $update['adminpassword'] = $res['adminpassword'];
        $update['note'] = $res['note'];
        $where['id'] = $res['id'];
        $list = M('site')->where($where)->save($update);
        if($list){
            $this->success("更新成功", U("Admin/Site/index"));exit;
        }else{
            $this->error("更新失败");exit;
        }
    }

    public function fpt(){
        $user_id = I('user_id');
        if(empty($user_id)){
            $this->ajaxreturn(array('state' => 0, 'data' => '信息错误'), 'json');
        }
        $where['user_id'] = $user_id;
        $list = M('site')->where($where)->field('id,site,ftp_ip,username,password,adminadd,adminusername,adminpassword,note')->find();
        if(empty($list)){
            $this->ajaxreturn(array('state' => 0, 'data' => 'FTP信息为空'), 'json');
        }else{
            $id = $list['id'];
            unset($list['id']);
            $this->ajaxreturn(array('state' => 1, 'data' => $list,'id'=>$id), 'json');
        }
    }

}